using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Info.Stock;
using EntLibEC.Info.Basic;
using EntLibEC.Biz;
using EntLibEC.Biz.Basic;
using EntLibEC.Biz.Stock;

namespace EntLibEC.View.Stock
{
	/// <summary>
	/// Summary description for TransferItemOpt.
	/// </summary>
	public partial class TransferItemOpt : PageBase
	{

		protected EntLibEC.View.Cmn.Stock ucStock;
		protected EntLibEC.View.Cmn.StockProduct ucProduct;

		private int sysno = AppConst.IntNull;
		private int productsysno = AppConst.IntNull;
		private string opt;
		protected string bodyOnLoad="";		
		private TransferInfo oInfo;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if ( Request.QueryString["sysno"] != null)
			{
				try
				{
					this.sysno = Convert.ToInt32(Request.QueryString["sysno"].ToString());
				}
				catch
				{
					Assert(lblMessage, "sysno is not an integer", -1);
					return;
				}
			}
			else
			{
				Assert(lblMessage, "sysno missing", -1);
			}

			if ( Request.QueryString["productsysno"] != null)
			{
				try
				{
					this.productsysno = Convert.ToInt32(Request.QueryString["productsysno"].ToString());
				}
				catch
				{
					Assert(lblMessage, "productsysno is not an integer", -1);
					return;
				}
			}

			if ( Request.QueryString["opt"] == null )
			{
				Assert(lblMessage, "opt missing", -1);
				return;
			}
			else
			{
				this.opt = Request.QueryString["opt"].ToString();
			}

			InitForm();
			if ( !Page.IsPostBack)
			{
				ucStock.BindStock(false);
				GetInfo();
				BindForm();
			}
		}
		private void GetInfo()
		{
			if ( base.sInfo.sTransfer == null || this.sysno != base.sInfo.sTransfer.SysNo)
			{	
				base.sInfo.sTransfer = TransferManager.GetInstance().Load(this.sysno);
			}
			oInfo = base.sInfo.sTransfer;
		}

		public void BindForm()
		{
			
			lblTransferNo.Text = oInfo.TransferID;
			ucStock.StockSysNo = oInfo.StockSysNo;
			

			if ( opt == "update" )
			{
				TransferItemInfo oItem = oInfo.itemHash[this.productsysno] as TransferItemInfo;
				if ( oItem == null )
				{
					Assert(lblMessage, "get item information failed", -1);
				}
				else
				{
					ucProduct.ProductSysNo = oItem.ProductSysNo;
					txtTransferQty.Text = oItem.TransferQty.ToString();
					scTransferItemType.TypeSysNo = oItem.TransferType;
					txtTransferCost.Text = oItem.TransferCost.ToString(AppConst.DecimalFormat);
					
				}
			}
			setTransferType();
			
		}
		private void setTransferType()
		{
			try
			{
				if ( scTransferItemType.TypeSysNo == (int)AppEnum.TransferItemType.Source)
				{
					txtTransferCost.Text = "auto";
					txtTransferCost.Enabled = false;
				}
				else
				{
					txtTransferCost.Text = "";
					txtTransferCost.Enabled = true;
				}
			}
			catch{}
		}
		public void InitForm()
		{
			btnUpdateItem.Visible = false;
			btnAddItem.Visible = false;

			ucStock.Enabled = false;
			txtTransferCost.Enabled = false;
			scTransferItemType.Enabled = false;

			if ( opt == "update")
			{
				btnUpdateItem.Visible = true;
				ucProduct.Enabled = false;
			}
			else
			{
				btnAddItem.Visible = true;
				scTransferItemType.Enabled = true;
			}
			
		}

		private bool CheckForm()
		{
			//productsysno
			//qty > 0
			//cost(if target >0 decimal)
			if ( ucProduct.ProductSysNo == AppConst.IntNull)
			{
				Assert(lblMessage, "product id or sysno can't be empty", -1);
				return false;
			}
			if ( txtTransferQty.Text.Trim() == "" )
			{
				Assert(lblMessage, "Qty can't be empty", -1);
				return false;
			}
			try
			{
				int qty = Convert.ToInt32(txtTransferQty.Text.Trim());
				if ( qty <= 0)
				{
					Assert(lblMessage, "qty shoud be > 0 ", -1);
					return false;
				}
			}
			catch
			{
				Assert(lblMessage, "Qty should be an integer", -1);
				return false;
			}
			if ( scTransferItemType.TypeSysNo == (int)AppEnum.TransferItemType.Target)
			{
				try
				{
					decimal cost = Convert.ToDecimal(txtTransferCost.Text.Trim());
//					if ( cost <= 0 )
//					{
//						Assert(lblMessage, "cost should be >0", -1);
//						return false;
//					}
				}
				catch
				{
					Assert(lblMessage, "cost should be a decimal", -1);
					return false;
				}
			}
			return true;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnAddItem_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.StTransferFillIn, false, lblMessage) )
				return;

			if (!CheckForm())
				return;
			try
			{
				GetInfo();
				TransferItemInfo oItem = new TransferItemInfo();
				
				oItem.ProductSysNo = ucProduct.ProductSysNo;
				oItem.TransferSysNo = this.sysno;
				oItem.TransferQty = Convert.ToInt32(txtTransferQty.Text.Trim());
				oItem.TransferType = scTransferItemType.TypeSysNo;
				if ( scTransferItemType.TypeSysNo == (int)AppEnum.TransferItemType.Target )
					oItem.TransferCost = Convert.ToDecimal(txtTransferCost.Text.Trim());
				
				TransferManager.GetInstance().InsertItem(oInfo, oItem);

				LogManager.GetInstance().Write(new LogInfo(oItem.SysNo, (int)AppEnum.LogType.St_Transfer_Item_Insert, base.sInfo));
				bodyOnLoad = "javascript:window.opener.__doPostBack('btnRefresh','btnRefresh');window.close(); return false;";
			}
			catch(BizException exp)
			{
				Assert(lblMessage, exp.Message, -1);
			}
			catch(Exception exp)
			{
				Assert(lblMessage, "insert transfer item error, see log", -1);
				ErrorLog.GetInstance().Write(exp.ToString());
			}
		}

		protected void btnUpdateItem_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.StTransferFillIn, false, lblMessage) )
				return;

			if (!CheckForm())
				return;
			try
			{
				GetInfo();
				TransferItemInfo newItem = new TransferItemInfo();
				newItem.ProductSysNo = ucProduct.ProductSysNo;
				newItem.TransferQty = Convert.ToInt32(txtTransferQty.Text.Trim());
				if ( scTransferItemType.TypeSysNo == (int)AppEnum.TransferItemType.Target )
					newItem.TransferCost = Convert.ToDecimal(txtTransferCost.Text.Trim());

				TransferManager.GetInstance().UpdateItem(oInfo, newItem);

				LogManager.GetInstance().Write(new LogInfo(newItem.SysNo, (int)AppEnum.LogType.St_Transfer_Item_Update, base.sInfo));
				bodyOnLoad = "javascript:window.opener.__doPostBack('btnRefresh','btnRefresh');window.close(); return false;";
			}
			catch(BizException exp)
			{
				Assert(lblMessage, exp.Message, -1);
			}
			catch(Exception exp)
			{
				Assert(lblMessage, "insert transfer item error, see log", -1);
				ErrorLog.GetInstance().Write(exp.ToString());
			}
		}

		protected void scTransferItemType_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			GetInfo();
			BindForm();
		}
	}
}
